import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/pages/test_detail/model/model_test_detail.dart';
import 'package:flutter_base/pages/test_questions/test_questions_logic.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:math';

//4 情绪选择器
class CircleOptionsWidget extends StatelessWidget {
  // final List<ModelTestDetailQuestionsOptions> options;
  // final List<int> selectedIds;
  // final Function(List<int>) onSelectionChanged;
  final TestQuestionsLogic logic;

  const CircleOptionsWidget({
    super.key,
    required this.logic,
    // required this.selectedIds,
    // required this.onSelectionChanged,
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        // final centerX = constraints.maxWidth / 2;
        // final centerY = constraints.maxHeight / 2;

        // 定义四个圆的半径
        final outerRadius = 121.5.w; // 243/2
        final middleRadius1 = 87.5.w; // 175/2
        final middleRadius2 = 57.5.w; // 115/2
        final innerRadius = 33.5.w; // 67/2

        return Stack(
          children: [
            // Positioned.(child: Container(color: Colors.red,)),
            // 绘制同心圆fill
            CustomPaint(
              size: Size(constraints.maxWidth, constraints.maxHeight),
              painter: CirclesPainter(
                radiuses: [
                  innerRadius,
                  middleRadius2,
                  middleRadius1,
                  outerRadius
                ],
              ),
            ),

            // 绘制各个选项
            ...List.generate((logic.currentQuestion?.options ?? []).length,
                (index) {
              // 计算选项的位置
              final option = (logic.currentQuestion?.options ?? [])[index];

              // 使用随机分布或均匀分布在整个视图中，而不仅限于圆形线圈
              // 随机生成位置，但确保不超出边界
              final random = Random(index); // 使用索引作为种子，确保每次构建位置一致

              // 根据索引确定悬浮球大小，实现不同大小
              double size;
              if (index % 3 == 0) {
                size = 50.w; // 小
              } else if (index % 3 == 1) {
                size = 60.w; // 中
              } else {
                size = 70.w; // 大
              }

              // 计算可用区域，考虑悬浮球大小，避免超出边界
              double maxX = constraints.maxWidth - size;
              double maxY = constraints.maxHeight - size;

              // 随机生成位置，但避免中心区域过度拥挤
              // double distance = 0;
              double x = 0;
              double y = 0;

              // 确保球体分布在整个视图中，避免过度集中
              if (index < (logic.currentQuestion?.options ?? []).length / 3) {
                // 前1/3的选项分布在外围区域
                x = random.nextDouble() * maxX * 0.8 + maxX * 0.1;
                y = random.nextDouble() * maxY * 0.3 + maxY * 0.7;
              } else if (index <
                  (logic.currentQuestion?.options ?? []).length * 2 / 3) {
                // 中间1/3的选项分布在中间区域
                x = random.nextDouble() * maxX * 0.6 + maxX * 0.2;
                y = random.nextDouble() * maxY * 0.6 + maxY * 0.2;
              } else {
                // 后1/3的选项分布在内部区域
                x = random.nextDouble() * maxX * 0.4 + maxX * 0.3;
                y = random.nextDouble() * maxY * 0.4 + maxY * 0.3;
              }

              // 判断是否选中
              final isSelected = logic.isOptionMultiSelected(option.id ?? 0);
              // option.id != null && selectedIds.contains(option.id!);

              return Positioned(
                left: x,
                top: y,
                child: GestureDetector(
                  onTap: () {
                    logic.toggleAnswer(option.id ?? 0);
                    //
                    // if (option.id != null) {
                    //   List<int> newSelectedIds = List<int>.from(selectedIds);
                    //   if (isSelected) {
                    //     newSelectedIds.remove(option.id!);
                    //   } else {
                    //     newSelectedIds.add(option.id!);
                    //   }
                    //   // 通知父组件选择变化
                    //   onSelectionChanged(newSelectedIds);
                    // }
                  },
                  child: Container(
                    width: size,
                    height: size,
                    padding: EdgeInsets.all(8.w),
                    // 添加内边距
                    decoration: BoxDecoration(
                      color:
                          isSelected ? AppColor.primary : AppColor.colorDBF1FF,
                      shape: BoxShape.circle,
                      boxShadow: [
                        BoxShadow(
                          color: AppColor.primary.withOpacity(0.1),
                          blurRadius: 5.w,
                          offset: Offset(0, 2.w),
                        ),
                      ],
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      option.content ?? '',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 12.sp,
                        fontWeight: FontWeight.w500,
                        color: isSelected ? Colors.white : AppColor.color7F909F,
                      ),
                    ),
                  ),
                ),
              );
            }),
          ],
        );
      },
    );
  }
}

// 绘制同心圆的自定义画笔
class CirclesPainter extends CustomPainter {
  final List<double> radiuses;

  CirclesPainter({required this.radiuses});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = AppColor.colorE4EBF0
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.0;

    final center = Offset(size.width / 2, size.height / 2);

    // 绘制同心圆
    for (double radius in radiuses) {
      canvas.drawCircle(center, radius, paint);
    }
  }

  @override
  bool shouldRepaint(CirclesPainter oldDelegate) => false;
}
